// pages/peixue/peixue.js
const app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },
  /**
   * 已购课程
   */
  getclass: function() {
    let that = this;
    wx.request({
      url: app.httpUrl + '/activity/queryChildActList',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: function(res) {
        console.log(res.data.data)
        let all_time = 0; //累计学时
        for (let a = 0; a < res.data.data.length; a++) {
          //获取单节课的学习时间
          res.data.data[a].studyTime = res.data.data[a].endDate - res.data.data[a].startDate;
          res.data.data[a].startDate = util.formatDate(res.data.data[a].startDate, 'Y年M月D日');
          res.data.data[a].endDate = util.formatDate(res.data.data[a].endDate, 'Y年M月D日');
          res.data.data[a].studyTime = util.formatDate(res.data.data[a].studyTime, 'h');
          all_time += parseInt(res.data.data[a].studyTime)
        }
        console.log(all_time)
        that.setData({
          class_list: res.data.data,
          all_time: all_time //累计学时
        })
        wx.hideLoading()
      }
    })
  },
  /**跳转活动中心 */
  activityLive: function(event) {
    var activityId = event.currentTarget.dataset.idx;
    var childId = event.currentTarget.dataset.childid;
    app.globalData.activityId = activityId;
    app.globalData.childId = childId;
    wx.switchTab({
      url: '/pages/study/study'
    })
  },


  /**
   * 获取已绑定宝贝
   */
  queryBindChild: function() {
    let that = this;
    wx.request({
      url: app.httpUrl + '/user/queryBindChild',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        console.log(res.data)
        that.setData({
          child: res.data
        })
      }
    })
  },
  /**
   * 获取用户信息
   */
  getUserInfo: function() {
    let that = this;
    if (app.globalData.appUser != null) {
      that.queryBindChild(); //获取已绑定宝贝
      that.getclass() //获取已购课程
      return;
    }
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.httpUrl + '/common/getUserInfo',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: "POST",
            success: function(res) {
              if (res.data.code == '000') {
                app.globalData.appUser = res.data.data;
                app.globalData.openid = res.data.data.openid;
                that.setData({
                  appUser: app.globalData.appUser
                })
                that.queryBindChild(); //获取已绑定宝贝
                that.getclass() //获取已购课程
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    this.getUserInfo()
  },
  /**
   * 跳转到宝贝档案
   */
  jump_baby: function(e) {
    let that = this
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    console.log(that.data.child[index])
    wx.setStorageSync('child', that.data.child[index] )
    wx.navigateTo({
      url: '/pages/baby_file/baby_file?id=' + id+'&types=2',
    })
  },
  /**
   * 跳转到添加宝贝页面
   */
  add: function() {
    wx.navigateTo({
      url: '/pages/addchild/addchild',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})